#include "RayTracer/Shapes/Plane.hpp"
#include "RayTracer/ParseTypes.hpp"


using namespace RayTracer;


bool Plane::Parse( Scene& s, JSONValue* v )
{
	Vector3	position;
	if( !::Parse( v, "Position", position ) )
		return false;

	if( !::Parse( v, "Normal", mNormal ) )
		return false;
	mNormal.Normalize();

	mDistance	=	position*mNormal;

	return Model::Parse( s, v );
}


bool Plane::Intersect( Vector3::ParamType_t origin, Vector3::ParamType_t dir, float& t ) const
{
	float	test	=	mNormal * Vector3( dir );

	if( test >= 0.0f )
		return false;

	float	dist	=	( -Math::Dot( mNormal, Vector3( origin ) ) + mDistance) / test;

	if( dist > 0.0f && dist < t )
	{
		t	=	dist;
		return true;
	}

	return false;
}


Vector3 Plane::Normal( const Vector3& hp ) const
{
	return mNormal;
}
